package com.x.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.x.controller.ScheduleController;
import com.x.mapper.ScheduleMapper;
import com.x.pojo.Schedule;
import com.x.service.ScheduleService;
import com.x.util.PageBean;
import com.x.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: ScheduleServiceImpl
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.09.15 17:06
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {


    @Autowired
    private ScheduleMapper scheduleMapper;



    @Override
    public R page(int pageSize, int currentPage) {
        //分页
        PageHelper.startPage(currentPage, pageSize);
        //查询
        List<Schedule> scheduleList = scheduleMapper.queryList();
        //分页数据装配
        PageInfo<Schedule> pageInfo = new PageInfo<>(scheduleList);

        //分页信息放到PageBean实体中
        PageBean<Schedule> pageBean = new PageBean<>(currentPage, pageSize, pageInfo.getTotal(), scheduleList);

        //PageBean放到r中
        R ok = R.ok(pageBean);

        return ok;
    }

    @Override
    public R remove(Integer id) {
        int rows = scheduleMapper.deleteById(id);
        if(rows>0)
            return R.ok(null);
        else
            return R.fail(null);
    }

    @Override
    public R save(Schedule schedule) {
        int rows = scheduleMapper.insert(schedule);
        if(rows>0)
            return R.ok(null);
        else
            return R.fail(null);
    }

    @Override
    public R update(Schedule schedule) {
        if(schedule.getId()==null)
            return R.fail(null);

        int rows = scheduleMapper.update(schedule);
        if(rows>0)
            return R.ok(null);
        else
            return R.fail(null);
    }

}
